using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Entity;
using T.FAS.Runtime.Expression.Service;
using T.FAS.Runtime.ScheduleJob.SPI;
using T.ServiceComponent.Kernel.Service;

namespace T.FAS.Runtime.ScheduleJob.Engine
{
    public class SubscriptionJobEngine : IScheduleJobImpEngineProxy, IScheduleJobExecuteHandler, IScheduleJobDataResultBuilder
    {
        public void AfterExecute(ScheduleJobExecContext execContext,TResult result)
        {

        }

        public void BeforeExecute(ScheduleJobExecContext execContext)
        {
        }

        public TResult BuildScheduleJobDataResult(ScheduleJobExecContext context, object dataResult)
        {
            return new TResult();
        }

        public object Execute(ScheduleJobExecContext context)
        {
            var sJob = context.ScheduleJobMetaData as Metadata.Entity.ScheduleJob.ScheduleJob;

            if (sJob == null)
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobMetadataNotExistError, CommonStringClassEngine.ActionJobEngine_NotValidSchedulaJobMetaData);
            if (sJob.JobType != Metadata.Entity.ScheduleJob.JobType.Subscription)
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobExecuteError, string.Format(CommonStringClassEngine.SubscriptionJobEngine_Execute1,sJob.Code));
            if (string.IsNullOrEmpty(sJob.JobContent))
                throw new FasException(ScheduleJobRTPExceptionCode.ScheduleJobExecuteError, string.Format(CommonStringClassEngine.ServerJSJobEngine_NotDefineTaskScript, sJob.Code));
            var tType = Type.GetType("T.FAS.Runtime.ScheduleJob.SubscriptionJobEngine.SubscriptionJobExecuteEngine,T.FAS.Runtime.ScheduleJob.SubscriptionJobEngine");
            var executor = Activator.CreateInstance(tType) as IScheduleJobExecutor;
            return executor.Execute(context);
        }

        public void ExecuteError(ScheduleJobExecContext execContext, Exception exception)
        {
        }
    }
}
